(* ::Package:: *)

BeginPackage["WolframLanguageServer`TableGenerator`"]
ClearAll[Evaluate[Context[] <> "*"]]


GenerateUnicodeTable::usage = "GenerateUnicodeTable[file_] generates unicode table to file."


Begin["`Private`"]
ClearAll[Evaluate[Context[] <> "*"]]


GenerateUnicodeTable[file_] := Module[
    {
        UnicodeCharacters = Import[FileNameJoin[{
            $InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources", "UnicodeCharacters.tr"
        }], "Text"],
        AliasToLongName, LongNameToUnicode
    },

    OpenWrite[file];

    WriteLine[file, "(* "~~"::Package::" ~~ " *)\n"];
    WriteLine[file, "(* This file is generated by WolframLanguageServer`TableGenerator` package. *)\n\n"];
    WriteLine[file, "BeginPackage[\"WolframLanguageServer`UnicodeTable`\"]"];
    WriteLine[file, "ClearAll[Evaluate[Context[] <> \"*\"]]\n\n"];

    AliasToLongName =
    UnicodeCharacters
    // StringCases[
        (("\\[" ~~ longName:(LetterCharacter...) ~~ "]") ~~
        Whitespace ~~ "(" ~~ alias:Shortest[___] ~~ ")") :> {
            alias
            // StringCases["$" ~~ shortname : Shortest[__] ~~ "$" :> shortname],
            longName
        }
    ]
    // DeleteCases[{{}, _}]
    // Map[Apply[Thread@*Rule]]
    // Flatten // Association;

    LongNameToUnicode = UnicodeCharacters
    // StringCases[(
        "0x" ~~ unicode:(HexadecimalCharacter..) ~~ Whitespace ~~
        ("\\[" ~~ longName:(LetterCharacter...) ~~ "]")
    ) :> (longName -> FromDigits[unicode, 16])]
    // Association;

    Keys[AliasToLongName]
    // Map[StringTake[#, 1]&]
    // DeleteDuplicates
    // DeleteCases[_?LetterQ]
    // Prepend["["]
    // DeleteDuplicates
    // (leaders \[Function] ( 
        Write[file, Unevaluated[WolframLanguageServer`UnicodeTable`UnicodeLeaders = leaders]];
        leaders
    ));

    WriteLine[file, "\n"];

    AliasToLongName
    // (assoc \[Function] (
        Write[file, Unevaluated[WolframLanguageServer`UnicodeTable`AliasToLongName = assoc]];
        assoc
    ));

    WriteLine[file, "\n"];

    LongNameToUnicode
    // (assoc \[Function] (
        Write[file, Unevaluated[WolframLanguageServer`UnicodeTable`LongNameToUnicode = assoc]];
        assoc
    ));

    (* Join[
        AliasToLongName
        // KeyValueMap[{alias, longName} \[Function] (
            <|
                "label" -> StringJoin[
                    LongNameToUnicode[longName]
                    // Replace[{
                        code_?(LessThan[16^^E000]) :> (
                            FromCharacterCode[code]
                        ),
                        _ -> " "
                    }], "\t",
                    alias, "\t\t",
                    longName
                ],
                "kind" -> 1,
                (* "detail" -> , *)
                "filterText" -> alias,
                "insertText" -> StringDrop[longName, 1],
                "data" -> <|"type" -> "Alias"|>
            |>
        )],
        LongNameToUnicode
        // KeyValueMap[{longName, unicode} \[Function] (
            <|
                "label" -> StringJoin[
                    unicode
                    // Replace[{
                        code_?(LessThan[16^^E000]) :> (
                            FromCharacterCode[code]
                        ),
                        _ -> " "
                    }], "\t",
                    longName
                ],
                "kind" -> 1,
                (* "detail" -> , *)
                "filterText" -> StringDrop[longName, 1],
                "insertText" -> StringDrop[longName, 1],
                "data" -> <|"type" -> "LongName"|>
            |>
        )]
    ]
    // (items \[Function] (
        Write[file, Unevaluated[WolframLanguageServer`UnicodeTable`UnicodeCompletionItems = items]]
    ));*)

    WriteLine[file, "\n"];
    WriteLine[file, "EndPackage[]"];

    Close[file];
     
]


End[]


EndPackage[]